package com.creditcmsplt.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GZIPUtil {

	private static Logger log = LoggerFactory.getLogger(GZIPUtil.class);
	
//	/**
//	 * 字符串压缩 别传入null，未做参数校验
//	 * 
//	 * @param str:要压缩的字符串
//	 * @return 返回压缩后的字符串--如果异常，返回null
//	 */
//	public static String compress(String str) {
//		if (str == null || str.length() == 0) {
//            return str;
//        }
//        ByteArrayOutputStream out = null;
//        GZIPOutputStream gzip = null;
//        String compress = "";
//        try {
//            out = new ByteArrayOutputStream();
//            gzip = new GZIPOutputStream(out);
//            gzip.write(str.getBytes());
//            gzip.close();
//            compress = out.toString("ISO-8859-1");//ISO-8859-1
//        } catch (IOException e) {
//        	log.error("",e);
//        } finally {
//            if (null != out) {
//                try {
//                    out.close();
//                } catch (IOException e) {
//                	log.error("",e);
//                }
//            }
//        }
//        return compress;
//	}
//
//	/**
//	 * 解压缩，别传入null，未做参数校验
//	 * 
//	 * @param str
//	 *            需要解压缩的字符串
//	 * @return 返回解压后的字符串--结果，如果程序出错返回null
//	 * @throws IOException
//	 */
//	public static String uncompress(String str) {
//		try {
//			if (str == null || str.length() == 0) {
//				return str;
//			}
//			ByteArrayOutputStream out = new ByteArrayOutputStream();
//			ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
//			GZIPInputStream gunzip = new GZIPInputStream(in);
//			byte[] buffer = new byte[256];
//			int n;
//			while ((n = gunzip.read(buffer)) >= 0) {
//				out.write(buffer, 0, n);
//			}
//			return out.toString();
//		} catch (Exception e) {
//			log.error("",e);
//		} 
//		return null;
//	}
//	
	
	public static final String GZIP_ENCODE_UTF_8 = "UTF-8";

    public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";


    /**
     * 字符串压缩为GZIP字节数组
     * 
     * @param str
     * @param encoding
     * @return
     */
    public static byte[] compressStr(String str, String encoding) {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes(encoding));
            gzip.close();
        } catch (IOException e) {
        	log.error("",e);
        }
        return out.toByteArray();
    }

    /**
     * GZIP解压缩
     * 
     * @param bytes
     * @return
     */
    public static byte[] uncompress(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream ungzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
        } catch (IOException e) {
        	log.error("",e);
        }

        return out.toByteArray();
    }

    /**
     * 
     * @param bytes
     * @return
     */
    public static String uncompressToString(byte[] bytes) {
        return uncompressToString(bytes, GZIP_ENCODE_UTF_8);
    }

    /**
     * 
     * @param bytes
     * @param encoding
     * @return
     */
    public static String uncompressToString(byte[] bytes, String encoding) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream ungzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            return out.toString(encoding);
        } catch (IOException e) {
        	log.error("",e);
        }
        return null;
    }
    
    public static String compress(String str,String encoding) {
    	byte[] bytes=compressStr(str, encoding);
    	try {
			String result=new String(bytes,Charset.forName(encoding));
			return result;
		} catch (Exception e) {
			log.error("",e);
		}
    	return null;
    }
    
    public static String uncompress(String str,String encoding) {
    	try {
			return uncompressToString(str.getBytes(Charset.forName(encoding)), encoding);
		} catch (Exception e) {
			log.error("",e);
		}
    	return null;
    }
}
